class ProductsController < ApplicationController

  before_filter :load_label, :load_category, :load_type

  def index
    if @category
      @products = Product.find_by_category( @category.id, @type.id )
    else
      @products = Product.paginate(:per_page => 10, :page => 1, :order => ' rand() ', :conditions => { :label_id => @label.id, :item_type_id => @type.id })
    end
    
    @title = "Lista de #{@type.name.pluralize}"
    
  end

  def show
    @product = Product.find(params[:id])
    @label = @product.label
    @categories = [@product.category]
    @title = "#{@type.name} - #{@product.name.capitalize}"
  end

  private
  
  def load_label
    if params[:label_id].blank?
      @label = Label.find_xerox
    else
      @label = Label.find( params[:label_id] )
    end
  end

  def load_category
    if params[:category_id]
      @category = Category.find( params[:category_id] )
      @categories = [ @category ]
    else
      @categories = Category.find_roots
    end
  end

  def load_type
    if params[:type_id].blank?
      @type = Type.find_produto
    else
      @type = Type.find( params[:type_id] )
    end
  end

end
